﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Library.AppWeb
{
    public class Compare
    {
        /// <summary>
        /// So sánh 2 ngày ( -1 : ngày 1 nhỏ hơn , 0 : bằng nhau , 1 : lớn hơn )
        /// </summary>
        /// <param name="compareto">Điều kiện so sánh tới</param>
        /// <param name="date1">Ngày 1</param>
        /// <param name="date2">Ngày 2</param>
        /// <returns>Giá trị trả về</returns>        
        public static int CompareTwoDate(CompareType compareto, DateTime date1, DateTime date2)
        {
            int result = -1;
            if (date1.Year < date2.Year) result = -1;
            if (date1.Year == date2.Year) result = 0;
            if (date1.Year > date2.Year) result = 1;
            if (compareto == CompareType.Year || result != 0) return result;

            if (date1.Month < date2.Month) result = -1;
            if (date1.Month == date2.Month) result = 0;
            if (date1.Month > date2.Month) result = 1;
            if (compareto == CompareType.Month || result != 0) return result;

            if (date1.Day < date2.Day) result = -1;
            if (date1.Day == date2.Day) result = 0;
            if (date1.Day > date2.Day) result = 1;
            if (compareto == CompareType.Day || result != 0) return result;

            if (date1.Hour < date2.Hour) result = -1;
            if (date1.Hour == date2.Hour) result = 0;
            if (date1.Hour > date2.Hour) result = 1;
            if (compareto == CompareType.Hours || result != 0) return result;

            if (date1.Minute < date2.Minute) result = -1;
            if (date1.Minute == date2.Minute) result = 0;
            if (date1.Minute > date2.Minute) result = 1;
            if (compareto == CompareType.Minutes || result != 0) return result;
                        
            return result;
        }

        /// <summary>
        /// So sánh 2 giờ (hh:MM:ss) ( -1 : giờ 1 nhỏ hơn , 0 : bằng nhau , 1 : lớn hơn )
        /// </summary>
        /// <param name="Hour1">Giờ 1</param>
        /// <param name="Hour2">Giờ 2</param>
        /// <returns></returns>
        public static int CompareTwoHour(DateTime Hour1, DateTime Hour2)
        {
            int result = -1;
            if (Hour1.Hour < Hour2.Hour) result = -1;
            if (Hour1.Hour == Hour2.Hour) result = 0;
            if (Hour1.Hour > Hour2.Hour) result = 1;
            if (result != 0) return result;

            if (Hour1.Minute < Hour2.Minute) result = -1;
            if (Hour1.Minute == Hour2.Minute) result = 0;
            if (Hour1.Minute > Hour2.Minute) result = 1;
            if (result != 0) return result;

            if (Hour1.Second < Hour2.Second) result = -1;
            if (Hour1.Second == Hour2.Second) result = 0;
            if (Hour1.Second > Hour2.Second) result = 1;
            if (result != 0) return result;
            
            return result;
        }

        public enum CompareType
        {
            Day,
            Month,
            Year,
            Hours,
            Minutes
        }        
    }
}
